//mesh
import MeshBase from "three-base/MeshBase";
import houseGLB from "./house.glb?url";
import utils from "@/utils/utils";
import { MeshStandardMaterial, Plane, Vector3 } from "three";
import Transition from "three-base/Transition";
import { autorun } from "mobx";
import homeStore from "../../homeStore";

// 房屋模型
export default class House extends MeshBase {
    constructor() {
        super();
        this.loadModel(houseGLB);
        this.init();
    }

    //从往上裁剪面
    clipPlane1 = new Plane(new Vector3(0, -1, 0), 0);
    // 从右边往左边裁剪
    clipPlane2 = new Plane(new Vector3(1, 0, 0), -22);
    // 从左边往右边裁剪
    clipPlane3 = new Plane(new Vector3(-1, 0, 0), -50);

    clipPlaneY = (() => {
        const clipPlaneY = new Transition({ y: 0 });
        clipPlaneY.setDuration(500);
        clipPlaneY.setBezier([0, 0, 1, 1]);
        clipPlaneY.onChange(({ y }) => {
            this.clipPlane1.constant = y;
        });

        autorun(() => {
            const floorY = [50, 13, 17, 20, 23, 27, 30, 34, 37, 42];
            clipPlaneY.set({ y: floorY[homeStore.showFlor] });
        });

    })();

    init() {

        this.addEventListener("loaded", () => {
            this.center(); // 居中
            this.alignBottom(); // 底对齐
            console.log(this);

            this.traverseMesh((mesh) => {

                (mesh.material as MeshStandardMaterial).metalness = 0;

                mesh.userData = {
                    cursor: "pointer",
                    enableEvent: true,
                };

                // 设置裁剪的位置
                if ([
                    "wuding", "qiangbi4", "网格3432_3", "boli1", "网格3435_7", "网格3450_6",
                    "chuanghu", "qiangbi2", "qiangbi5", "网格3451_3", "网格3449_1", "网格3448_3",
                    ""
                ].includes(mesh.name)) {
                    // mesh.visible = false;
                    (mesh.material as MeshStandardMaterial).clippingPlanes = [this.clipPlane1, this.clipPlane2, this.clipPlane3];
                    (mesh.material as MeshStandardMaterial).clipIntersection = true;
                }

                mesh.addEventListener("dbclick", (e: any) => {
                    console.log(mesh.name, e.point);
                });

            });
        });
    }



}